package com.ayoka.parenting.ui.coach.applycoach;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.SpinnerOptionBean;
import com.ayoka.parenting.databinding.ActivityApplyCoachBinding;
import com.ayoka.parenting.mvp.MVPBaseActivity;
import com.jincp.common.model.ToolBarOptions;
import com.jincp.common.utils.DateUtil;
import com.jincp.common.utils.MyLogger;
import com.wheelpicker.picker.AddressPickerDialog;
import com.wheelpicker.picker.DatePickerDialog;

import java.util.Date;
import java.util.List;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: ApplyCoachActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class ApplyCoachActivity extends MVPBaseActivity<ActivityApplyCoachBinding, ApplyCoachContract.View, ApplyCoachPresenter> implements ApplyCoachContract.View {

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, ApplyCoachActivity.class);
        activity.startActivity(intent);
    }

    private String province, city;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_apply_coach;
    }

    @Override
    protected void initViews() {
        initSpinner(mPresenter.getApplyTypeOptionData(), mDataBinding.spinnerApplyType);
        initSpinner(mPresenter.getDegreeOptionData(), mDataBinding.spinnerDegree);
        initSpinner(mPresenter.getParentOptionData(), mDataBinding.spinnerParent);
        initSpinner(mPresenter.getChildrenOptionData(), mDataBinding.spinnerChildren);
        initSpinner(mPresenter.getCompleteOptionData(), mDataBinding.spinnerCompleteMeditation);
        mDataBinding.tvBirthDate.setOnClickListener(this);
        mDataBinding.tvProvince.setOnClickListener(this);
        mDataBinding.btnSubmit.setOnClickListener(this);
    }

    private void initSpinner(List<SpinnerOptionBean> data, Spinner spinner) {
        ArrayAdapter<SpinnerOptionBean> spinnerAdapter = new ArrayAdapter<SpinnerOptionBean>(getContext(), R.layout.spinner_item_white_textcolor, R.id.tv_content) {
            @Override
            public int getCount() {
                return super.getCount() - 1;
            }
        };
        spinnerAdapter.addAll(data);
        spinnerAdapter.setDropDownViewResource(R.layout.spinner_drop_item_white_textcolor);
        spinner.setAdapter(spinnerAdapter);
        spinner.setSelection(data.size() - 1, true);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions(R.string.apply_haba_coach);
        options.backgroundResource = 0;
        options.navigateId = R.mipmap.icon_back_white;
        options.titleColor = Color.WHITE;
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);
    }

    @Override
    protected ApplyCoachPresenter getPresenter() {
        return new ApplyCoachPresenter();
    }

    @Override
    public void onClick(View view) {
        if (view == mDataBinding.tvProvince) {
            if (mPresenter != null) {
                mPresenter.showAddressSelectorDialog(getSupportFragmentManager());
                mDataBinding.tvProvince.setEnabled(false);
            }
        } else if (view == mDataBinding.tvBirthDate) {
            if (mPresenter != null) {
                mPresenter.showSelectorDateDialog(getSupportFragmentManager());
                mDataBinding.tvBirthDate.setEnabled(false);
            }
        } else if (view == mDataBinding.btnSubmit) {
            // 姓名
            if (mDataBinding.edtName.length() == 0) {
                showToast(getString(R.string.hint_input_name));
                return;
            }
            String name = mDataBinding.edtName.getText().toString().trim();
            // 申请类型
            SpinnerOptionBean applyTypeBean = mPresenter.getApplyTypeOptionData().get(mDataBinding.spinnerApplyType.getSelectedItemPosition());
            if (applyTypeBean == null || (int)applyTypeBean.value == -1){
                showToast(getString(R.string.hint_pick_apply_type));
                return;
            }
            int applyType = (int) applyTypeBean.value;
            // 出生年月
            if (mDataBinding.tvBirthDate.length() == 0) {
                showToast(getString(R.string.hint_pick_birth_date));
                return;
            }
            String birthDate = mDataBinding.tvBirthDate.getText().toString().trim();
            // 学历
            SpinnerOptionBean degreeBean = mPresenter.getDegreeOptionData().get(mDataBinding.spinnerDegree.getSelectedItemPosition());
            if (degreeBean == null || (int)degreeBean.value == -1){
                showToast(getString(R.string.hint_pick_degree));
                return;
            }
            int degree = (int) degreeBean.value;
            // 所在省份
            if (mDataBinding.tvProvince.length() == 0) {
                showToast(getString(R.string.hint_pick_province));
                return;
            }
            // 目前工作
            if (mDataBinding.edtWork.length() == 0) {
                showToast(getString(R.string.hint_input_work));
                return;
            }
            String work = mDataBinding.edtWork.getText().toString().trim();
            // 联系方式
            if (mDataBinding.edtContractMobile.length() == 0) {
                showToast(getString(R.string.hint_input_contract_mobile));
                return;
            }
            String contractMobile = mDataBinding.edtContractMobile.getText().toString().trim();
            // 电子邮件
            if (mDataBinding.edtEmail.length() == 0) {
                showToast(getString(R.string.hint_input_email));
                return;
            }
            String email = mDataBinding.edtEmail.getText().toString().trim();
            // 联系地址
            if (mDataBinding.edtContractAddress.length() == 0) {
                showToast(getString(R.string.hint_input_contract_address));
                return;
            }
            String contractAddress = mDataBinding.edtContractAddress.getText().toString().trim();
            // 你的身份 爸爸或者妈妈
            SpinnerOptionBean parentBean = mPresenter.getParentOptionData().get(mDataBinding.spinnerParent.getSelectedItemPosition());
            if (parentBean == null || (int)parentBean.value == -1){
                showToast(getString(R.string.hint_pick_parent));
                return;
            }
            int parent = (int) parentBean.value;
            // 你有几个孩子
            SpinnerOptionBean childrenBean = mPresenter.getChildrenOptionData().get(mDataBinding.spinnerChildren.getSelectedItemPosition());
            if (childrenBean == null || (int)childrenBean.value == -1){
                showToast(getString(R.string.hint_pick_children));
                return;
            }
            int children = (int) childrenBean.value;
            // 微信号
            if (mDataBinding.edtWechatNumber.length() == 0) {
                showToast(getString(R.string.hint_input_wechat_number));
                return;
            }
            String wechatNumber = mDataBinding.edtWechatNumber.getText().toString().trim();
            // 你是否完成了课程
            SpinnerOptionBean completeBean = mPresenter.getCompleteOptionData().get(mDataBinding.spinnerCompleteMeditation.getSelectedItemPosition());
            if (completeBean == null || (int)completeBean.value == -1){
                showToast(getString(R.string.hint_pick_parent));
                return;
            }
            int complete = (int) completeBean.value;
            // 相关经历
            if (mDataBinding.edtExperience.length() == 0) {
                showToast(getString(R.string.hint_input_experience));
                return;
            }
            String experience = mDataBinding.edtExperience.getText().toString().trim();
            String extra = mDataBinding.edtExtra.getText().toString().trim();
            if (mPresenter != null) {
                mPresenter.submitApplyCoach(name, applyType, birthDate, degree, province, city, work, contractMobile, email, contractAddress, parent, children, wechatNumber, complete, experience, extra);
                mDataBinding.btnSubmit.setEnabled(false);
            }
        }
    }

    @Override
    public void onAddressSelected(String province, String city, String area, String street) {
        mDataBinding.tvProvince.setText(province + city);
        this.province = province;
        this.city = city;
    }

    @Override
    public void onDialogDismiss(DialogFragment dialog) {
        if (dialog instanceof DatePickerDialog) {
            mDataBinding.tvBirthDate.setEnabled(true);
        } else if (dialog instanceof AddressPickerDialog) {
            mDataBinding.tvProvince.setEnabled(true);
        }
    }

    @Override
    public void onSelectorDateCallback(long result) {
        try {
            Date date = DateUtil.getDate(result);
            String dateText = DateUtil.formatDate(date, "yyyy-MM-dd");
            mDataBinding.tvBirthDate.setText(dateText);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onSubmitApplyCoachCallback(boolean isSuccess) {
        if (isSuccess){
            finishDelay();
        }
        mDataBinding.btnSubmit.setEnabled(true);
    }
}
